module MySQLMigrationHelpers
  FK_SETTING = {:no_action => "no action", :set_null =>"set null", :cascade =>"cascade", :restrict =>"restrict"}

  def fk(from_table, from_column, to_table, on_delete=FK_SETTING[ :no_action ], on_update=FK_SETTING[ :no_action ])
    execute %(alter table #{from_table}
          add constraint #{constraint_name(from_table, from_column)}
          foreign key (#{from_column})
          references #{to_table}(id)
          on delete #{on_delete}
          on update #{on_update})
  end

  def drop_fk(from_table, from_column)
    execute %(alter table #{from_table}
          drop foreign key #{constraint_name(from_table, from_column)})
  end

  def constraint_name(table, column)
    "fk_#{table}_#{column}"
  end


end